package com.bjwl.controller.safty;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjwl.base.PageParams;
import com.bjwl.base.Result;
import com.bjwl.model.Role;
import com.bjwl.service.safty.RoleService;
import com.bjwl.view.MenuView;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping("/safty/role")
public class RoleController {
	//声明业务层对象
	@Autowired
	private RoleService roleService;
	
	//根据获取角色信息 全查 分页查询
	@GetMapping("/queryRole")
	public Map<String,Object> queryRole(PageParams pageParams){
		PageInfo<Role> roles=roleService.queryRole(pageParams);
		//创建一个Map集合
		Map<String,Object> map=new HashMap<>();
		map.put("total", roles.getTotal());
		map.put("rows", roles.getList());
		return map;
	}
	
	//启用角色-将特定角色的状态变为55
	@PutMapping("/enableRole/{ro_id}")
	public Result enableRole(@PathVariable String ro_id){
		Integer roId = Integer.parseInt(ro_id);
		System.out.println(ro_id);
		boolean bo=roleService.enableRole(roId);
		if(bo==true) {
			return Result.success("启用角色成功");
		} 
		return Result.fail("系统升级中");
	}
	
	//禁用角色-将特定角色的状态变为77
	@PutMapping("/disableRole")
//	@RequestParam 这个是表单数据提交的注解
	public Result disableRole(@RequestBody String ro_id){
//		System.out.println("fff:"+ro_id);
		String roId = JSON.parseObject(ro_id).get("ro_id").toString();
		System.out.println("qqqqq:"+roId);
		boolean bo=roleService.disableRole(Integer.parseInt(roId));
		if(bo==true) {
			return Result.success("禁用角色成功");
		}
		return Result.fail("系统升级中");
	}
	
	//添加角色
	@PostMapping("/addRole")
	public Result addRole(@RequestBody Role role){
		System.out.println("addrole:"+role);
		boolean bo=roleService.addRole(role);
		if(bo==true) {
			return Result.success("添加角色成功");
		}
		return Result.fail("系统升级中");
	}
	//编辑角色
	@PutMapping("/editRole")
	public Result editRole(@RequestBody Role role){
		System.out.println("editrole:"+role);
		boolean bo=roleService.editRole(role);
		if(bo==true) {
			return Result.success("更新角色成功");
		}
		return Result.fail("系统升级中");
	}
	//角色授权,获取所有角色主权限子权限
	@GetMapping("/queryAllRole")
	public List<MenuView> queryAllRole(){
		List<MenuView> menuList=roleService.queryModuleAll();
		return menuList;
	}
	//角色授权,获取所有角色主权限子权限
	@GetMapping("/queryAllRoleById")
	public List<Integer> queryAllRoleById(Integer ro_id){
		List<Integer> moduleIdList=roleService.queryAllRoleById(ro_id);
		return moduleIdList;
	}
	//角色授权提交
	@PutMapping("/rightRoleById")
	public Result rightRoleById(@RequestBody JSONObject json){
		System.out.println(json);
		Integer roId=json.getInteger("ro_id");
		JSONArray roleIdList1=json.getJSONArray("moduleByIdList");
		//JSONArray roleIdList=roleIdList1.toArray();
		String json1 = json.getString("moduleByIdList");
		List<Integer> roleIdList= (List<Integer>)JSONArray.parseArray(json1,Integer.class);
		System.out.println("roid:"+ roId+"---moduleByIdList"+roleIdList);
//		int num=roleIdList.getClass().getDeclaredFields().length;
//		System.out.println("roleIdList:"+fields.length);
		//通过 角色id改变，角色权限；先根据id删除相应权限，再根据id添加权限
		Boolean bo=roleService.updateRoleRight(roId,roleIdList);
		if(bo==true) {
			return Result.success("修改成功", roleIdList);
		}
		
		return Result.fail("系统升级中");
	}
}
